package cn.linjianhui.article.service.impl;

import cn.linjianhui.article.dao.ContextMapper;
import cn.linjianhui.article.model.Article;
import cn.linjianhui.article.model.ArticleDetail;
import cn.linjianhui.article.model.ArticlePageQuery;
import cn.linjianhui.article.model.PageData;
import cn.linjianhui.article.model.entity.ContextDO;
import cn.linjianhui.article.service.ArticleService;
import cn.linjianhui.article.utils.MapUtil;
import cn.linjianhui.article.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ContextMapper contextMapper;

    @Override
    public PageData<Article> search(ArticlePageQuery query) {
        return PageUtil.mapToPageData(contextMapper.queryByKey(PageUtil.fromPageQuery(query),
                query.getType(), query.getKey()), Article.class);
    }

    @Override
    public ArticleDetail getDetail(long id) {
        ContextDO context = contextMapper.selectById(id);
        return MapUtil.mapToObj(context, ArticleDetail.class);
    }
}
